package UI
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	/**
	 * Button
	 * 背景图片包含3种状态，正常，鼠标悬浮，鼠标点击 
	 * @author jianhong
	 */  
	public class Button extends Sprite
	{
		private		const	NORMAL:int = 0;
		private		const	OVER:int = 1;
		private		const	CLICK:int = 2;
		
		private		var	_width:int = 0;
		private		var _height:int = 0;
		private		var	_textField:TextField = null;
		
		private		var _normalBgImg:BitmapData = null;
		private		var _overBgImg:BitmapData = null;
		private		var _clickBgImg:BitmapData = null;
		
		private		var _rendLayer:BitmapData = null;
		
		public function Button(width:int, height:int, img:Bitmap)
		{
			super();
			
			this._width = width;
			this._height = height;
	
			var bgImgWidth:int = img.bitmapData.width/3;
			var bgImgHeight:int = img.bitmapData.height;
			this._normalBgImg = new BitmapData(bgImgWidth, bgImgHeight, true, 0xffffffff);
			this._normalBgImg.copyPixels(img.bitmapData, new Rectangle(0, 0, bgImgWidth, bgImgHeight), new Point());
			
			this._overBgImg = new BitmapData(bgImgWidth, bgImgHeight, true, 0xffffffff);
			this._overBgImg.copyPixels(img.bitmapData, new Rectangle(bgImgWidth, 0, bgImgWidth, bgImgHeight), new Point());
			
			this._clickBgImg = new BitmapData(bgImgWidth, bgImgHeight, true, 0xffffffff);
			this._clickBgImg.copyPixels(img.bitmapData, new Rectangle(bgImgWidth*2, 0, bgImgWidth, bgImgHeight), new Point());
			
			this._rendLayer = new BitmapData(this._width, this._height, true, 0x00ffffff);
			var bgImg:Bitmap = new Bitmap(this._rendLayer);
			this.addChild(bgImg);
			
			this._textField = new TextField();
			this._textField.width = this._width;
			this._textField.height = this._height;
			this._textField.autoSize = TextFieldAutoSize.CENTER;
			this.addChild(this._textField);
			
			this.scrollRect = new Rectangle(0, 0, this._width, this._height);
			this.mouseChildren = false;
			this.renderBg(this.NORMAL);
			
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		public function	onMouseOver(evt:MouseEvent):void{
			this.renderBg(this.OVER);
		}
		
		public	function onMouseDown(evt:MouseEvent):void{
			this.renderBg(this.CLICK);
		}
		
		public	function onMouseUp(evt:MouseEvent):void{
			this.renderBg(this.OVER);
		}
		
		public	function onMouseOut(evt:MouseEvent):void{
			this.renderBg(this.NORMAL);	
		}
		
		public	function	set text(txt:String):void{
			this._textField.text = txt;
		}
		
		public	function	set textColor(color:uint):void{
			this._textField.textColor = color;
		}
		
		private	function	renderBg(status:int):void{
			var srcWidth:Number = this._normalBgImg.width;
			var srcHeight:Number = this._normalBgImg.height;
			
			var matrix:Matrix = new Matrix();
			matrix.scale(this._width/srcWidth, this._height/srcHeight);
			
			var srcBitmap:BitmapData = null;
			
			if (status == this.NORMAL){
				srcBitmap = this._normalBgImg;
			}
			else if(status == this.OVER){
				srcBitmap = this._overBgImg;
			}
			else if(status == this.CLICK){
				srcBitmap = this._clickBgImg;
			}
			this._rendLayer.draw(srcBitmap, matrix, null, null, null, true);
		}
	}
}